import numpy as np
import sys
import time


start_time = time.perf_counter()
# takes in command-line arguments [a, b, n]
a = float(sys.argv[1])
b = float(sys.argv[2])
n = int(sys.argv[3])
# a = 0
# b = 1
# n = 10

# define the f(x)
def f(x):
    return x ** 2


# define integral function
def integrateRange(a, b, n):
    """
    Numerically integrate with the trapezoid rule on the interval from
    a to b with n trapezoids.
    """
    sum_f = -(f(a) + f(b)) / 2
    # sum of all linespace
    for x in np.linspace(a, b, n):
        sum_f = sum_f + f(x)
    integral = sum_f * (b - a) / n
    return integral


integral = integrateRange(a, b, n)
print(
    f"With n = {n} trapezoids, our estimate of the integral from {a} to {b} is {integral}")
# print(np.linspace(0, 1, 10))
stop_time = time.perf_counter()
print(f"Time cost: {stop_time - start_time:.8f}")